package com.dexilaonian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.ui.BaseActivity;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.Logutil;
import com.dexilaonian.utils.Okhttp;
import com.dexilaonian.utils.PreferencesUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class SetActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.base_topText)
    TextView baseTopText;
    @Bind(R.id.rl_change_pwd)
    RelativeLayout rlChangePwd;
    @Bind(R.id.rl_about)
    RelativeLayout rlAbout;
    @Bind(R.id.btn_tuichu)
    Button btnTuichu;
    private String phone;

    @Override
    public int getLayoutID() {
        return R.layout.activity_set;
    }

    @Override
    public void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        baseTopText.setText("设置");
        Intent intent = getIntent();
        phone = intent.getStringExtra("Phone");

    }

    @OnClick({R.id.btn_tuichu,R.id.back,R.id.base_topText, R.id.rl_change_pwd, R.id.rl_about})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
            case R.id.base_topText:
                finish();
                break;
            case R.id.btn_tuichu:
                initQuit();
                break;

            case R.id.rl_change_pwd:
                Intent intent = new Intent(SetActivity.this, ChangePwdActivity.class);
                if (!TextUtils.isEmpty(phone)) {
                    intent.putExtra("PHONE", phone);
                }
                startActivity(intent);
                break;
            case R.id.rl_about:
                startActivity(new Intent(SetActivity.this, AboutUsActivity.class));
                break;
        }
    }

    private void initQuit() {
        showProgressBar(true);
        String quit_url = Constant.base_url + "/user/signout.do";
        Map<String, String> map = new HashMap<>();
        Okhttp.postString(true, quit_url, map, new Okhttp.CallBac() {
            private int status;
            @Override
            public void onError(Call call, Exception e, String state, int id) {
                showProgressBar(false);
            }
            @Override
            public void onResponse(String response, int id) {
                showProgressBar(false);
                Logutil.e(response);
                try {
                    JSONObject json = new JSONObject(response);
                    status = Integer.parseInt(json.optString("status"));
                    if (status == 34||status ==16) {
                        startActivity(new Intent(SetActivity.this, LoginActivity.class));
                        return;
                    }
                    if (status == 0) {
                        Intent intent = new Intent(SetActivity.this, LoginActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        if (!TextUtils.isEmpty(phone)) {
                            intent.putExtra("PHONE", phone);
                        }
                        startActivity(intent);
                        finish();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onNoNetwork(String state) {
                showProgressBar(false);
            }
        });
    }
}
